﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace XFGameFramework.Localization
{ 
    /// <summary>
    /// 图片信息
    /// </summary>
    [Serializable]
    public class ImageInfo 
    {
        /// <summary>
        /// 资源模块名称
        /// </summary>
        public string projectName;
        
        /// <summary>
        /// 图片名称
        /// </summary>
        public string imageName;
    }

    /// <summary>
    /// 多语言类型
    /// </summary>
    public enum MultiLanguageType 
    { 
        /// <summary>
        /// 文本
        /// </summary>
        String,
        /// <summary>
        /// 图片
        /// </summary>
        Image, 
        /// <summary>
        /// 网络图片
        /// </summary>
        NetworkImage
    }

    /// <summary>
    /// 多语言信息
    /// </summary>
    [Serializable]
    public class LanguageInfo 
    {
        /// <summary>
        /// 唯一标识
        /// </summary>
        public int id; 

        /// <summary>
        /// 类型
        /// </summary>
        public MultiLanguageType type = MultiLanguageType.String;

        /// <summary>
        /// 文本(type为MultiLanguageType.String时有效)
        /// </summary>
        public string str;

        /// <summary>
        /// 图片信息(type为MultiLanguageType.Image时有效)
        /// </summary>
        public ImageInfo image;

        /// <summary>
        /// 网络图片地址(type为MultiLanguageType.NetworkImage 时有效)
        /// </summary>
        public string url;

        /// <summary>
        /// 备注
        /// </summary>
        public string notes;
    }

    /// <summary>
    /// 某一种语言的配置数据
    /// </summary> 
    public class LanguageConfig : ScriptableObject
    {
        [Header("语言")]
        [Tooltip("语言")]
        public SystemLanguage language;

        [Header("显示名称")]
        [Tooltip("显示名称")]
        public string displayName;

        [Header("语言配置")]
        [Tooltip("语言配置")]
        public List<LanguageInfo> languagesInfo = new List<LanguageInfo>();
        

        private Dictionary<int,LanguageInfo> language_dic = new Dictionary<int,LanguageInfo>();

#if UNITY_EDITOR

        public void Rename() 
        {
            string asset_path = AssetDatabase.GetAssetPath(this);
            AssetDatabase.RenameAsset(asset_path, language.ToString());
        }

        public void Save() 
        { 
            EditorUtility.SetDirty(this); 
        }

#endif

        /// <summary>
        /// 查询语言信息
        /// </summary>
        /// <param name="id">唯一标识</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public LanguageInfo GetLanguageInfo(int id) 
        {
            if (language_dic.Count == 0) 
            {
                foreach (var item in languagesInfo)
                {
                    if (language_dic.ContainsKey(item.id))
                        throw new Exception(string.Format("多语言配置id:{0}重复!",id));
                    language_dic.Add(item.id, item);
                }    
            }

            if(language_dic.ContainsKey(id))
                return language_dic[id];

            return null;
        }

        public void ClearCache() 
        {
            language_dic.Clear();
        }

    }
}
